﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.App_Code;
using System.Linq;
using Manager.FormAdd_Edit;
using System.Data.Linq;
using Manager.ViewModels;
using Manager.Common;

namespace Manager.FormMain
{
    public partial class Frm_BarButton_HangHoaDichVu : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();
        List<DichVu> lstDichVu;

        public Frm_BarButton_HangHoaDichVu()
        {
            InitializeComponent();
            lstDichVu = new List<DichVu>();
        }

        private void Frm_BarButton_HangHoaDichVu_Load(object sender, EventArgs e)
        {
            if (lstDichVu.Count > 0)
                lstDichVu.Clear();
            db.Refresh(RefreshMode.OverwriteCurrentValues, db.DichVus);
            var lst = db.DichVus.GroupBy(t => t.NhomDichVu1.TenNhomDV).ToList();
            foreach (var group in lst)
            {
                foreach (var dv in group)
                {
                    if (dv.MaDV == 8)//dich vu khac
                        continue;
                    lstDichVu.Add(dv);
                }
            }
            gridControl1.DataSource = null;
            gridControl1.DataSource = lstDichVu;
        }

        private void butThemMoi_Click(object sender, EventArgs e)
        {
            frm_HangHoaDichVu frmHangHoa = new frm_HangHoaDichVu();
            frmHangHoa.ShowDialog();
            Frm_BarButton_HangHoaDichVu_Load(null, null);
        }

        private void butHieuChinh_Click(object sender, EventArgs e)
        {
            int index = gridView1.FocusedRowHandle;
            var dichVu = gridView1.GetRow(index) as DichVu;

            frm_HangHoaDichVu frmHangHoaDichVu = new frm_HangHoaDichVu(dichVu);
            frmHangHoaDichVu.ShowDialog();
            Frm_BarButton_HangHoaDichVu_Load(null, null);
        }

        private void butXoa_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr = MessageBox.Show("Bạn có muốn xóa không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    int index = gridView1.FocusedRowHandle;
                    var dichVu = gridView1.GetRow(index) as DichVu;
                    db.DichVus.DeleteOnSubmit(dichVu);
                    db.SubmitChanges();
                    MessageBox.Show("Xóa dữ liệu thành công!");
                    this.Frm_BarButton_HangHoaDichVu_Load(sender, e);
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("Frm_BarButton_HangHoaDichVu:butXoa_Click" + ex.Message + "===>" + ex.StackTrace);
                MessageBox.Show("Có lỗi xảy ra!!!");
            }
        }       

        private void Frm_BarButton_HangHoaDichVu_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F3)
                this.butThemMoi_Click(sender, e);
            if (e.KeyCode == Keys.F5)
                this.butHieuChinh_Click(sender, e);
            if (e.KeyCode == Keys.F7)
                this.butXoa_Click(sender, e);
        }
    }
}